/**
 * 保存页面滚动位置
 * @type {{onScrollPositionHandle(): void}}
 */
const methods = {
  onScrollPositionHandle(e) {
    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    this.scroll = scrollTop;
  },
  setScrollPosition() {
    if (this.scroll > 0) {
      window.scrollTo(0, this.scroll);
      this.scroll = 0;
    }
  },
  removeScrollPosition() {
    window.removeEventListener('scroll', this.onScrollPositionHandle)
  },
  bindScrollPosition() {
    window.addEventListener('scroll', this.onScrollPositionHandle, false);
  }
};

const beforeRouteLeave = function (to, from, next) {
  this.removeScrollPosition();
  next(true);
};

const beforeRouteEnter = function (to, from, next) {
  next(vm => {
    vm.bindScrollPosition();
  })
}


const beforeDestroy = function () {
  this.removeScrollPosition();
};

const activated = function () {
  this.bindScrollPosition();
  this.setScrollPosition();
};

const data = function () {
  return {}
};
export default {
  methods,
  data,
  activated,
  beforeDestroy,
  beforeRouteLeave,
  beforeRouteEnter
}
